package org.jiamandan.com.strategy.impl.tram.listener;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jiamandan.com.constant.JSYConstant;
import org.jiamandan.com.dto.JsyBasicDto;
import org.jiamandan.com.strategy.impl.tram.IJSYDZBasicListener;
import org.jiamandan.com.utils.JsyObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Jmd

 * @see:
 * @创建日期：2023-11-28 22:34
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-11-28 22:34	         Jmd        	创建
 * @end
 */
@Service(value = JSYConstant.Service.CMD_JSY_T00204_SERVICE)
@AllArgsConstructor
@Slf4j
public class JSYActualStatusListener implements IJSYDZBasicListener {

    @Override
    public void doExecute(String payload, ChannelHandlerContext ctx) throws Exception {
        log.info("JMD-JSY充电实时上报数据T00204:" + payload);

        JsyBasicDto jsyBasicDto = JsyObjectUtil.parse(payload);
        String pileNo = jsyBasicDto.getDeviceNo();

        // 应答实时数据 开始
        StringBuilder builder = new StringBuilder();
        builder.append("_" + JSYConstant.Cmd.S00204).append("/0002:01").append("\r\n");
        log.info("JMD-JSY设备号：【" + pileNo + "】实时数据应答(CMD = S00204):" + builder.toString());
        log.info("===实时应答="+ctx.channel().id()+"===="+pileNo+"=========");
        ByteBuf buf = Unpooled.buffer(builder.toString().getBytes().length);
        buf.writeBytes(builder.toString().getBytes());
        ChannelFuture future = ctx.writeAndFlush(buf);
        // 应答实时数据 结束
        log.info("TODO===实时应答数据，修改响应数据===截取字符串进行判断===TODO");
    }
}
